var MessageUtil = /** @class */ (function () {
    function MessageUtil() {
        // 保存所有message实例
        this.messageInstances = [];
        // 递增编号
        this.currentIndex = 0;
        // Alert 类型
        this.type = {
            success: "alert-success",
            primary: "alert-primary",
            secondary: "alert-secondary",
            danger: "alert-danger",
            warning: "alert-warning",
            info: "alert-info",
            light: "alert-light",
            dark: "alert-dark"
        };
        // 创建图标
        this.icons = {
            success: function () {
                var path1 = "M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z";
                var path2 = "M10.97 4.97a.235.235 0 0 0-.02.022L7.477 9.417 5.384 7.323a.75.75 0 0 0-1.06 1.06L6.97 11.03a.75.75 0 0 0 1.079-.02l3.992-4.99a.75.75 0 0 0-1.071-1.05z";
                return IconUtil.createSVGByClassAndPaths("bi bi-check-circle", [
                    IconUtil.createPATH(path1),
                    IconUtil.createPATH(path2)
                ]);
            },
            danger: function () {
                var path1 = "M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z";
                var path2 = "M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708z";
                return IconUtil.createSVGByClassAndPaths("bi bi-x-circle", [
                    IconUtil.createPATH(path1),
                    IconUtil.createPATH(path2)
                ]);
            },
            warning: function () {
                var path1 = "M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z";
                var path2 = "M7.002 11a1 1 0 1 1 2 0 1 1 0 0 1-2 0zM7.1 4.995a.905.905 0 1 1 1.8 0l-.35 3.507a.552.552 0 0 1-1.1 0L7.1 4.995z";
                return IconUtil.createSVGByClassAndPaths("bi bi-exclamation-circle", [
                    IconUtil.createPATH(path1),
                    IconUtil.createPATH(path2)
                ]);
            },
            info: function () {
                var path1 = "M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z";
                var path2 = "M8.93 6.588l-2.29.287-.082.38.45.083c.294.07.352.176.288.469l-.738 3.468c-.194.897.105 1.319.808 1.319.545 0 1.178-.252 1.465-.598l.088-.416c-.2.176-.492.246-.686.246-.275 0-.375-.193-.304-.533L8.93 6.588zM9 4.5a1 1 0 1 1-2 0 1 1 0 0 1 2 0z";
                return IconUtil.createSVGByClassAndPaths("bi bi-info-circle", [
                    IconUtil.createPATH(path1),
                    IconUtil.createPATH(path2)
                ]);
            }
        };
    }
    MessageUtil.prototype.info = function (options) {
        options.type = 'info';
        this.createMessage(options);
    };
    MessageUtil.prototype.success = function (options) {
        options.type = 'success';
        this.createMessage(options);
    };
    MessageUtil.prototype.warn = function (options) {
        options.type = 'warning';
        this.createMessage(options);
    };
    MessageUtil.prototype.error = function (options) {
        options.type = 'danger';
        this.createMessage(options);
    };
    MessageUtil.prototype.createMessage = function (options) {
        var _this_1 = this;
        if (options === void 0) { options = { time: 3, type: "info" }; }
        var message = this.createContainer(options.type);
        if (options.msg == "") {
            options.msg = " ";
        }
        if (options.msg) {
            var messageContainer = document.createElement("span");
            var SVGContainer = document.createElement("span");
            SVGContainer.style.paddingRight = "4px";
            if (options.type == "success") {
                SVGContainer.appendChild(this.icons.success());
            }
            else if (options.type == "warning") {
                SVGContainer.appendChild(this.icons.warning());
            }
            else if (options.type == "danger") {
                SVGContainer.appendChild(this.icons.danger());
            }
            else {
                SVGContainer.appendChild(this.icons.info());
            }
            message.appendChild(SVGContainer);
            messageContainer.append(options.msg);
            messageContainer.style.textOverflow = "ellipsis";
            messageContainer.style.whiteSpace = "nowrap";
            messageContainer.style.overflow = "hidden";
            message.appendChild(messageContainer);
        }
        message.classList.add("alert-message-fade-in");
        var id = this.currentIndex + 1;
        var messageInstance = {
            id: id,
            el: message
        };
        this.messageInstances.push(messageInstance);
        this.currentIndex = id;
        message.id = "alert-" + id;
        if (options.showClose) {
            message.appendChild(this.createCloseButton(message.id));
        }
        message.style.top = (this.messageInstances.length - 1) * 60 + 10 + "px";
        // 插入到body中
        document.body.appendChild(message);
        setTimeout(function () {
            message.classList.remove("alert-message-fade-in");
        }, 200);
        var _this = this;
        // 时间未设置时，默认3s，时间设置为0则为不关闭
        if (!options.time && options.time != 0) {
            options.time = 3; // 默认3秒
        }
        // 如果时间设置大于0，怎会自动关闭
        if (options.time > 0) {
            messageInstance['timerId'] = setTimeout(function () {
                _this_1.removeMessage(message);
            }, options.time * 1000);
        }
    };
    MessageUtil.prototype.createContainer = function (type) {
        var container = document.createElement("div");
        container.className = "alert alert-dismissible alert-message fade show";
        container.setAttribute("role", "alert");
        if (type) {
            container.classList.add(this.type[type]);
        }
        else {
            container.classList.add(this.type.info);
        }
        return container;
    };
    // 创建关闭按钮
    MessageUtil.prototype.createCloseButton = function (messageId) {
        var _this_1 = this;
        var button = document.createElement("button");
        button.type = "button";
        button.classList.add("close");
        button.setAttribute("aria-label", "Close");
        button.innerHTML = "&times;";
        button.addEventListener("click", function () {
            var instanceIndex = -1;
            _this_1.messageInstances.forEach(function (instance, index) {
                if (instanceIndex < 0 && instance.el.id === messageId) {
                    instanceIndex = index;
                }
            });
            if (instanceIndex >= 0) {
                // 清除消息关闭定时
                _this_1.messageInstances[instanceIndex].el.classList.add("alert-message-fade-out");
                setTimeout(function () {
                    if (_this_1.messageInstances[instanceIndex].timerId > 0) {
                        clearTimeout(_this_1.messageInstances[instanceIndex].timerId);
                    }
                    _this_1.messageInstances[instanceIndex].el.remove();
                    var instance = _this_1.messageInstances.splice(instanceIndex, 1);
                    _this_1.refreshPosition();
                }, 200);
            }
            else {
                // 无操作
            }
        });
        return button;
    };
    // 移除消息实例
    MessageUtil.prototype.removeMessage = function (message) {
        var _this_1 = this;
        if (message) {
            message.classList.add("alert-message-fade-out");
            setTimeout(function () {
                message.remove();
                var messageIndex = -1;
                _this_1.messageInstances.forEach(function (instance, index) {
                    if (messageIndex < 0 && instance.el.id === message.id) {
                        messageIndex = index;
                    }
                });
                if (messageIndex >= 0) {
                    // 清除消息关闭定时
                    clearTimeout(_this_1.messageInstances[messageIndex].timerId);
                    _this_1.messageInstances.splice(messageIndex, 1);
                }
                _this_1.refreshPosition();
            }, 400);
        }
    };
    // 获取消息框距离顶部的距离，index从0开始
    MessageUtil.prototype.getMessageTop = function (index) {
        return index * 60 + 10 + "px";
    };
    // 刷新Alert的位置  
    MessageUtil.prototype.refreshPosition = function () {
        for (var i = 0; i < this.messageInstances.length; i++) {
            var topValue = this.getMessageTop(i);
            this.messageInstances[i].el.style.top = topValue;
        }
    };
    return MessageUtil;
}());
var IconUtil = {
    createSVGByClassAndPaths: function (classList, paths) {
        return IconUtil.createSVG("16", "16", "currentColor", classList, "0 0 16 16", paths);
    },
    createSVG: function (width, height, fill, classList, viewBox, paths) {
        var SVG = document.createElementNS("http://www.w3.org/2000/svg", "svg");
        SVG.setAttribute("width", width);
        SVG.setAttribute("height", height);
        SVG.setAttribute("fill", fill);
        // SVG.setAttribute("class", classList);
        SVG.setAttribute("viewBox", viewBox);
        if (paths.length > 0) {
            paths.forEach(function (p) {
                SVG.appendChild(p);
            });
        }
        return SVG;
    },
    createPATH: function (d) {
        var PATH = document.createElementNS("http://www.w3.org/2000/svg", "path");
        PATH.setAttribute("d", d);
        return PATH;
    }
};
